package com.ruoyi.project.app.controller;

import com.ruoyi.project.app.intercepter.FilterAnnotation;
import com.ruoyi.project.app.interfaces.FaceRequestInterface;
import com.ruoyi.project.app.request.FaceVerifyRequest;
import com.ruoyi.project.app.util.XResult;
import com.ruoyi.project.beautyflower.member.domain.Member;
import com.ruoyi.project.beautyflower.member.service.IMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/app/face")
@Api(tags = "三方接口-旷视模块")
public class AppFaceIdController implements FaceRequestInterface {
    @Autowired
    private IMemberService memberService;
    @Autowired
    private CacheManager cacheManager;

    @PostMapping("token")
    @FilterAnnotation(validator = true)
    @ApiOperation(notes = "token", response = String.class, value = "人脸识别token获取")
    public Object token(@ApiIgnore @RequestAttribute Integer userId) throws Exception {
        Member member = memberService.selectMemberById(userId);
        Object token = requestVerifyToken(member.getName(), member.getCarId());
        // 后台存储 biz_token
        Cache cache = cacheManager.getCache("system");
        cache.put(cacheKey + userId, token);
        return new XResult("获取成功成功", token);
    }

    @PostMapping("verify")
    @FilterAnnotation(validator = true)
    @ApiOperation(notes = "verify", value = "人脸识别验证")
    public Object verify(FaceVerifyRequest request, @ApiIgnore @RequestAttribute Integer userId) throws Exception {
        //return new XResult("验证成功", requestVerifyUrlOk(request.getBizToken(), request.getFile().getBytes()));
        // 后台存储获取 biz_token
        Cache cache = cacheManager.getCache("system");
        String token = cache.get(cacheKey + userId, String.class);
        return new XResult("验证成功", requestVerifyUrlOk(token, request.getFileBase64().getBytes()));
    }

}
